//
//  WealthViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/13.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "WealthViewController.h"
#import "BSSquareCell.h"
#import "MyInvestViewController.h"//我的投资
#import "MyPayViewController.h"//我要充值
#import "WithdrawalViewController.h"//我要提现
#import "AccountSetting.h"//账户设置

#import "WelcomeLoginViewController.h"//登录页面
#import "LoginViewController.h"//注册
#import "GetUserTotalModel.h"

@interface WealthViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *buttonVIew;
@property (nonatomic ,weak) UICollectionView *collectionView;

@property (nonatomic,strong)UIView *myBackView;

@property (nonatomic, strong) UIWindow *window;


@property (weak, nonatomic) IBOutlet UILabel *TotalAmount;//总资产

@property (weak, nonatomic) IBOutlet UILabel *Blance;//可用余额

@property (weak, nonatomic) IBOutlet UILabel *Aggregate;//投资金额
@property (weak, nonatomic) IBOutlet UILabel *totalReturn;//累计总收益
@property (weak, nonatomic) IBOutlet UILabel *freeze;//冻结金额

/**
 *  存储用户信息
 */
//@property (nonatomic, strong) NSMutableDictionary *userInfoDicM;
//
//@property (nonatomic, strong) NSMutableDictionary *amountDicM;

@property (nonatomic, strong) NSMutableArray *amountArr;//存储用户信息

@end

@implementation WealthViewController
static NSString *const ID = @"cell";
#define BSScreenW [UIScreen mainScreen].bounds.size.width
#define  cellWH  ((BSScreenW - (cols - 1) * margin) / cols)

#define SubTitleArr @[@"我的投资",@"我的借款",@"我要充值",@"我要提现",@"我要还款",@"债权转让",@"资金记录",@"邀请有奖",@"我的福利",@"账户设置"]
#define ImageArr @[@"classify8",@"classify9",@"classify10",@"classify11",@"classify12",@"classify13",@"classify14",@"classify15",@"classify16",@"classify17"]


- (NSMutableArray *)amountArr {
    if (!_amountArr) {
        _amountArr = [NSMutableArray array];
    }
    return _amountArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setUpFootView];
    
}

- (void)setUpFootView
{
    // 1.创建流水布局
    UICollectionViewFlowLayout *layout = ({
        
        // 1.创建流水布局
        UICollectionViewFlowLayout *layout =  [[UICollectionViewFlowLayout alloc] init];
        
        // 2.设置流水布局属性
        
        // 2.1设置cell尺寸
        layout.itemSize = CGSizeMake(Screen_Width/4, Screen_Width/4-5);
        
        // 2.2 设置cell之间间距
        layout.minimumInteritemSpacing =  0;
        layout.minimumLineSpacing = 0;
        
        layout;
    });
    
    // 2.创建UICollectionView
    UICollectionView *collectionView = ({
        
        // 1.创建UICollectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 400) collectionViewLayout:layout];
        
        // 2.设置数据源 -> 要UICollectionView展示数据
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        
        // 3. 注册cell
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BSSquareCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
        
        // 4.不允许collectionView滚动
        // 取消弹簧效果
        //        collectionView.bounces = NO;
        collectionView.scrollEnabled = NO;
        collectionView;
        
    });
    
    _collectionView = collectionView;
    
    // 3.设置collectionView为tableView的footView
    // 设置tableView的footView,高度由我们自己决定,宽度,位置都是控制不了
    [self.buttonVIew addSubview:collectionView];
    
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSSquareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    // 获取模型
   // BSSquareItem *item = self.squareList[indexPath.row];
    
   // cell.item = item;
    cell.backgroundColor = [UIColor whiteColor];
    cell.nameView.text = SubTitleArr[indexPath.item];
    cell.iconView.image = [UIImage imageNamed:ImageArr[indexPath.item]];
    [cell.layer setBorderWidth:0.5];
    [cell.layer setBorderColor:[UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1.00].CGColor];
    
    return cell;
}

//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    /**
     *  我的投资  0
     *  我的借款  1
     *  我要还款  4
     *  债权转让  5
     *  资金记录  6
     */
    if (indexPath.item == 0 || indexPath.item == 1 || indexPath.item == 4 || indexPath.row == 5 || indexPath.item == 6 || indexPath.item == 8) {
        MyInvestViewController *investView =[[MyInvestViewController alloc] init];
        [self setHidesBottomBarWhenPushed:YES];
        investView.num = indexPath.item;
        [self.navigationController pushViewController:investView animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
    }else if(indexPath.item == 2){
        
        //我要充值
        MyPayViewController *myPayView =[[MyPayViewController alloc] init];
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:myPayView animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
    }else if (indexPath.item == 3){
        //我要提现
        WithdrawalViewController *withdrawalView =[[WithdrawalViewController alloc] init];
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:withdrawalView animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
    }
    else if (indexPath.row == 9){
        //账户设置
        AccountSetting *accountView =[[AccountSetting alloc] init];
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:accountView animated:YES];
        [self setHidesBottomBarWhenPushed:NO];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_myBackView removeFromSuperview];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    //判断是否登录
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"]) {
        
        [self setUpWindow];
        return;
    }
    
    [self getUserInfo];
    
}

//用户资金统计接口
- (void)getUserInfo {
    
    //User.GetUserTotal
    /**
     *  用户id
     */  
    

    NSString *loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginId"];
    NSDictionary *dict = @{
                           @"LoginId":loginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUserTotal" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSDictionary *dict = [object[@"Data"] firstObject];
        GetUserTotalModel *model = [GetUserTotalModel mj_objectWithKeyValues:dict];
        [self.amountArr addObject:model];
        [self setDataWithModel:model];
        
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
    
}

//个人资金取值
- (void)setDataWithModel:(GetUserTotalModel *)model{
    
    _TotalAmount.text = [NSString stringWithFormat:@"%.2f",[model.TotalAmount floatValue]];//总资产
    _Blance.text = [NSString stringWithFormat:@"%.2f",[model.Blance floatValue]];//可用余额
    _totalReturn.text = [NSString stringWithFormat:@"%.2f",[model.totalReturn floatValue]];//累计总收益
    _Aggregate.text = [NSString stringWithFormat:@"%.2f",[model.Aggregate floatValue]];//投资累计金额
    self.freeze.text = [NSString stringWithFormat:@"%.2f", -[model.freeze floatValue]];//冻结金额
    
    [[NSUserDefaults standardUserDefaults] setObject:_Blance.text forKey:@"Blance"];
}


- (void)setUpWindow{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    _window = window;
    [UIView  animateWithDuration:0.2 animations:^{
        _myBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, Screen_Width, Screen_Height-20-49)];
        _myBackView.backgroundColor = [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:0.5];
        [window addSubview:_myBackView];
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    loginBtn.frame = CGRectMake(30, Screen_Height-120, (Screen_Width-90)/2, 30);
    registerBtn.frame = CGRectMake(60+(Screen_Width-90)/2, Screen_Height-120, (Screen_Width-90)/2, 30);
    
    [_myBackView addSubview:loginBtn];
    [_myBackView addSubview:registerBtn];
    
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    
    [loginBtn setBackgroundColor:[UIColor whiteColor]];
    [loginBtn setTitleColor:[UIColor colorWithRed:0.93 green:0.36 blue:0.29 alpha:1.00] forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:[UIColor colorWithRed:0.93 green:0.36 blue:0.29 alpha:1.00]];
    loginBtn.layer.cornerRadius = 15;
    registerBtn.layer.cornerRadius = 15;
    
    [loginBtn addTarget:self action:@selector(touchLogin:) forControlEvents:UIControlEventTouchUpInside];
    [registerBtn addTarget:self action:@selector(touchRegister:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

//跳转到登录页面
- (void)touchLogin:(UIButton *)btn{
    WelcomeLoginViewController *loginView = [[WelcomeLoginViewController alloc] init];
    [_myBackView removeFromSuperview];
    [self.navigationController pushViewController:loginView animated:YES];
}
//跳转到注册页面
- (void)touchRegister:(UIButton *)btn{
    LoginViewController *registerView = [[LoginViewController alloc] init];
    [_myBackView removeFromSuperview];
    
    [self.navigationController pushViewController:registerView animated:YES];
}



@end
